package cn.hx.mallorder.controller;

import cn.hx.mallorder.entity.OmsOrder;
import cn.hx.mallorder.entity.OmsOrderItem;
import cn.hx.mallorder.service.OmsOrderItemService;
import cn.hx.mallorder.service.OmsOrderService;
import cn.hx.mallorder.util.JwtUtils;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class AllOrderController {
    @Resource
    private OmsOrderService orderService;
    @Resource
    private ProductClient productClient;
    @Resource
    private OmsOrderItemService omsOrderItemService;
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @GetMapping("allOrder")
    public String index(Model model,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = 0L;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }
        UmsMemberVo umsMemberVo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        Long id2 = orderService.findId(umsMemberVo.getId());
        Integer id1 = omsOrderItemService.findItemById(id2);
        if (id1 == null) {
            id1 = 8;
        }
        List<Object> sList = productClient.getList(id1);
        String userName = umsMemberVo.getUsername();
        List<OmsOrder> list = new ArrayList<>();
        if (userName != null && !userName.equals("")) {
            list = orderService.queryByUserName(userName);
        }
        if (list!=null){
            model.addAttribute("list", list);
        }else {
            list=null;
            model.addAttribute("list", list);
        }
        model.addAttribute("sList", sList);
        model.addAttribute("t2", (Integer) 2);
        model.addAttribute("s10", 10);
        return "all-order";
    }

    @GetMapping("/timeChange")
    public String timeChange(Integer t, Model model,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = 0L;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }
        UmsMemberVo umsMemberVo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        Long id2 = orderService.findId(umsMemberVo.getId());
        Integer id1 = omsOrderItemService.findItemById(id2);
        if (id1 == null) {
            id1 = 8;
        }
        List<Object> sList = productClient.getList(id1);
        String userName = umsMemberVo.getUsername();
        List<OmsOrder> list = new ArrayList<>();
        if (userName != null && !userName.equals("")) {
            list = orderService.queryByUserName(userName);
        }
        model.addAttribute("sList", sList);
        List<OmsOrder> list1 = null;
        if (list!=null){
            if (t == 2) {
                model.addAttribute("t2", (Integer) 2);
                model.addAttribute("list", list);
                model.addAttribute("s10", 10);
                return "all-order";
            } else {
                Date date = new Date();
                if (t == 0) {
                    list1 = list.stream().filter(o -> {
                        return date.getTime() < o.getCreateTime().getTime() + 1000 * 60 * 60 * 24 * 90L;
                    }).collect(Collectors.toList());
                    model.addAttribute("t0", 0);
                }
                if (t == 1) {
                    list1 = list.stream().filter(o -> {
                        return date.getTime() < o.getCreateTime().getTime() + 1000 * 60 * 60 * 24 * 365L;
                    }).collect(Collectors.toList());
                    model.addAttribute("t1", 1);
                }
                model.addAttribute("list", list1);
                model.addAttribute("s10", 10);
                return "all-order";
            }
        }else {
            model.addAttribute("list", list);
            model.addAttribute("s10", 10);
            return "all-order";
        }
    }

    @GetMapping("/statusChange")
    public String statusChange(Integer s, Model model,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = 0L;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }
        UmsMemberVo umsMemberVo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        Long id2 = orderService.findId(umsMemberVo.getId());
        Integer id1 = omsOrderItemService.findItemById(id2);
        if (id1 == null) {
            id1 = 8;
        }
        List<Object> sList = productClient.getList(id1);
        String userName = umsMemberVo.getUsername();
        List<OmsOrder> list = new ArrayList<>();
        if (userName != null && !userName.equals("")) {
            list = orderService.queryByUserName(userName);
        }
        model.addAttribute("sList", sList);
        List<OmsOrder> list1 = null;
        if (list!=null){
            if (s == 10) {
                model.addAttribute("s10", (Integer) 10);
                model.addAttribute("list", list);
                return "all-order";
            } else {
                if (s == 1) {
                    list1 = list.stream().filter(o -> {
                        return o.getStatus() == 1;
                    }).collect(Collectors.toList());
                    model.addAttribute("s1", 1);
                }
                if (s == 2) {
                    list1 = list.stream().filter(o -> {
                        return o.getStatus() == 2;
                    }).collect(Collectors.toList());
                    model.addAttribute("s2", 2);
                }
                if (s == 3) {
                    list1 = list.stream().filter(o -> {
                        return o.getStatus() == 3;
                    }).collect(Collectors.toList());
                    model.addAttribute("s3", 3);
                }
                if (s == 4) {
                    list1 = list.stream().filter(o -> {
                        return o.getStatus() == 4;
                    }).collect(Collectors.toList());
                    model.addAttribute("s4", 4);
                }
                model.addAttribute("list", list1);
                return "all-order";
            }
        }else {
            model.addAttribute("s10", (Integer) 10);
            model.addAttribute("list", list);
            return "all-order";
        }
    }

    /*申请退货*/
    @GetMapping("/goBackOrder")
    public String goBackOrder(Integer id, Model model) {
        OmsOrder allById = new OmsOrder();
        allById.setStatus(4);
        orderService.update(allById, new QueryWrapper<OmsOrder>().eq("id", id));
        return "redirect:/allOrder";
    }

    /*确认收货*/
    @GetMapping("/haveOrder")
    public String haveOrder(Integer id, Model model) {
        OmsOrder allById = new OmsOrder();
        allById.setStatus(3);
        orderService.update(allById, new QueryWrapper<OmsOrder>().eq("id", id));
        return "redirect:/allOrder";
    }

    /*删除订单*/
    @GetMapping("allOrder/delOrder")
    public String delOrder(Long id) {
        OmsOrder allById = orderService.findAllById(id);
        if (allById.getOmsOrderItems() != null) {
            for (OmsOrderItem omsOrderItem : allById.getOmsOrderItems()) {
                omsOrderItemService.deleteByOrderItemId(omsOrderItem.getId());
            }
        }
        orderService.deleteById(id);
        return "redirect:/allOrder";
    }

    /*重新购买*/
    @GetMapping("/onceOrder")
    public String onceOrder(Integer id) {
        OmsOrder allById = new OmsOrder();
        allById.setStatus(0);
        orderService.update(allById, new QueryWrapper<OmsOrder>().eq("id", id));
        return "redirect:/allOrder";
    }
    /**/

}
